﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using WXYW.BLL;
using WXYW.DAL;
using WXYW.Model;
using System.Collections;

namespace WXYW
{
    public partial class ManageComponentPrice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindAccessoryGridView();
            }
        }

        private void BindAccessoryGridView()
        {
            AccessoryService accessoryService = new AccessoryService();
            AccessoryListGridView.DataSource = accessoryService.GetAccessoryList(keyWordTextBox.Text);
            AccessoryListGridView.DataBind();
        }

        protected void AccessoryListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }

            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= AccessoryListGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (AccessoryListGridView.PageIndex + 1).ToString();
            }
        }

        protected void AccessoryListGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("ChangePrice"))
            {
                CurrentComponetID.Value = e.CommandArgument.ToString();

                AccessoryService accessoryService = new AccessoryService();
                Accessory accessoryEntity = accessoryService.GetAccessoryByID(Convert.ToInt32(e.CommandArgument));
                ComponentModelNOTxtBox.Text = accessoryEntity.ModelNO;
                CHPriceTxtBox.Text = accessoryEntity.CBPrice.HasValue ? accessoryEntity.CBPrice.Value.ToString() : "";
                SFPriceTxtBox.Text = accessoryEntity.Price.HasValue ? accessoryEntity.Price.Value.ToString() : "";
                OriginalModelNO.Value = accessoryEntity.ModelNO;
                AccessoryPricePopupExtender.Show();
            }
        }

        protected void SavePriceLnkBtn_Click(object sender, EventArgs e)
        {
            AccessoryService accessoryService = new AccessoryService();
            Accessory accessoryEntity = accessoryService.GetAccessoryByID(Convert.ToInt32(Convert.ToInt32(CurrentComponetID.Value)));
            accessoryEntity.CBPrice = Convert.ToDecimal(CHPriceTxtBox.Text);
            accessoryEntity.Price = Convert.ToDecimal(SFPriceTxtBox.Text);
            accessoryService.UpdateAccessory(accessoryEntity);

            if (!OriginalModelNO.Value.Equals(ComponentModelNOTxtBox.Text.Trim()))
            {
                accessoryService.UpdateAccessoryModelNO(OriginalModelNO.Value, ComponentModelNOTxtBox.Text.Trim());
            }
            BindAccessoryGridView();
        }

        protected void AccessoryListGridView_DataBound(object sender, EventArgs e)
        {
            if (AccessoryListGridView.BottomPagerRow != null)
            {
                AccessoryListGridView.BottomPagerRow.Visible = true;
            }
        }

        protected void AccessoryListGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < AccessoryListGridView.PageCount)
            {
                AccessoryListGridView.PageIndex = e.NewPageIndex;
            }
            
            BindAccessoryGridView();
        }

        protected void AccessoryListGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            AccessoryService accessoryService = new AccessoryService();
            IQueryable<AccessoryModel> forms = accessoryService.GetAccessoryList("").AsQueryable();
            string sortDirection = "asc";
            if (Session["OrderDirection"] != null)
            {
                sortDirection = Session["OrderDirection"].ToString();
                if (sortDirection.Equals("asc"))
                {
                    Session["OrderDirection"] = "desc";
                }
                else
                {
                    Session["OrderDirection"] = "asc";
                }
            }
            else
            {
                Session["OrderDirection"] = "asc";
            }
            AccessoryListGridView.DataSource = forms.OrderBy(string.Format("{0} {1}", e.SortExpression, sortDirection)).ToList();
            AccessoryListGridView.DataBind();
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            AccessoryListGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindAccessoryGridView();
        }

        protected IEnumerable AccessoryListGridView_MustAddARow(System.Collections.IEnumerable data)
        {
            List<AccessoryModel> dds = (List<AccessoryModel>)data;
            dds.Add(new AccessoryModel());
            return dds;
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            BindAccessoryGridView();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = "";
            BindAccessoryGridView();
        }
    }
}
